//
//  DishManager.m
//  takeout
//
//  Created by Derekc on 12-3-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DishManager.h"
#import "ResolveXML.h"
#import "Constants.h"
#import "DishCategory.h"
#import "Dish.h"
#import "DishItem.h"

@implementation DishManager

-(NSArray *)getAllDishesBySupId:(NSString *)supplierId{
    NSString *URIString = [NSString stringWithFormat:SERVICE_DOMAIN, [NSString stringWithFormat: SERVICE_ALL_DISHES_BY_SUPPLIER,supplierId]];
    NSURL *url = [[NSURL alloc] initWithString:[URIString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"url%@",url);
    NSMutableURLRequest* request = [NSMutableURLRequest new];
    [request setURL:url];
    [request setHTTPMethod:@"GET"];
    NSHTTPURLResponse* response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString* responseXMLResult=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult options:0 error:&error];
    NSArray *dishCategories = [doc.rootElement children];
    NSMutableArray *result = [[NSMutableArray alloc]init];
    for (GDataXMLElement *element in dishCategories) {
        if ([element isKindOfClass:[GDataXMLElement class]]) {
            DishCategory *dishCategory = [[DishCategory alloc] init];
            dishCategory.dishCategoryId = [[[element childAtIndex:0] stringValue] integerValue];
            dishCategory.dishCategoryName = [[element childAtIndex:1] stringValue];
            //获得菜色
            GDataXMLElement *dishElements = (GDataXMLElement *)[element childAtIndex:2];
            NSArray *dishArray = [dishElements children];
            if ([dishArray count] > 0)
            {
                NSMutableArray *dishes =[[NSMutableArray alloc]init];
                for (GDataXMLElement *dishElement in dishArray) {
                    if ([dishElement isKindOfClass:[GDataXMLElement class]]) {
                        Dish *dish = [[Dish alloc] init];
                        dish.dishId = [[[dishElement childAtIndex:0] stringValue] integerValue];
                        dish.dishName = [[dishElement childAtIndex:1] stringValue];
//                        NSLog(@"name%@",dish.dishName);
                        dish.imgUrl = [[dishElement childAtIndex:2] stringValue];
                        dish.desc = [[dishElement childAtIndex:3] stringValue];
                        dish.price = [[[dishElement childAtIndex:4] stringValue] floatValue];
                        dish.secondPrice = [[[dishElement childAtIndex:5] stringValue] floatValue];
                        dish.thirdPrice = [[[dishElement childAtIndex:6] stringValue] floatValue];
                        dish.dishCategoryId =  [[[dishElement childAtIndex:7] stringValue] integerValue];
                        
                        //获得菜色类型
                        GDataXMLElement *dishItemElements = (GDataXMLElement *)[dishElement childAtIndex:8];
                        NSArray *dishItemArray = [dishItemElements children];
                        if ([dishItemArray count] > 0)
                        {
                            NSMutableArray *dishItems =[[NSMutableArray alloc]init];
                            for (GDataXMLElement *dishItemElement in dishItemArray) {
                                if ([dishItemElement isKindOfClass:[GDataXMLElement class]]) {
                                    DishItem *dishItem=[[DishItem alloc]init];
                                    dishItem.dishItemId=[[[dishItemElement childAtIndex:0] stringValue] integerValue];
                                    dishItem.price=[[[dishItemElement childAtIndex:1] stringValue] floatValue];
                                    dishItem.size=[[dishItemElement childAtIndex:2] stringValue];
                                    dishItem.type=[[dishItemElement childAtIndex:3] stringValue];
                                    [dishItems addObject:dishItem];
//                                    NSLog(@"id：%d价格：%f大小：%@冷热：%@",dishItem.dishItemId,dishItem.price,dishItem.size,dishItem.type);
                                }
                            }
                            dish.dishItems=dishItems;
                        }
                        [dishes addObject:dish];
                    }
                }
                dishCategory.dishes = dishes;
                [result addObject:dishCategory];
            }
            
            //            //获得菜色类型
            
            
        }
    }
    return result;
}

@end
